extends layout
append scripts
    script.
        console.log(document.cookie);
        document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";
        // expires 添加一个过期时间 (以 UTC 或 GMT 时间) 默认情况下, cookie 在浏览器关闭时删除
        // 使用 path 参数告诉浏览器 cookie 的路径, 默认情况下, cookie 属于当前页面
        console.log(document.cookie);
    script.
        function setCookie(cname, cvalue, exdays) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); // cookie 存活时间 30 天
            var expires = "expires=" + d.toGMTString();
            document.cookie = cname + "=" + cvalue + "; " + expires;
        }
        function getCookie(cname) {
            var name = cname + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i].trim(); // 删去前后空格
                if (c.indexOf(name) == 0) return c.substring(name.length, c.length);  // 如果能找到, 返回 cookie 的值
            }
            return "";
        }
        function checkCookie() {
            var user = getCookie("username");
            if (user != "") {
            alert("Welcome again " + user);
            } else {
                user = prompt("Please enter your name:", "");
                if (user != "" && user != null) {
                    setCookie("username", user, 30);
                }
            }
        }
block content
